高阶函数是对其他函数进行操作的函数,可以将它们作为参数或返回它们。 简单来说,高阶函数是一个函数,它接收函数作为参数或将函数作为输出返回。
// function fn(){
// console.log("这是fn函数 ...");
// }
//
// //封装一个能够适用于多数场景的业务函数 => 10 个地方
// function abc(fn){
// console.log(fn);
// fn();
// }
//
// abc(fn);
// var arr = [1,3,5];
// var arr2 = arr.map(function(item){
// return item * item;
// });
//计算一个月的30天的开销 , 需要记录每一天的开销 ,月末结算
// var costMoney = 0;//总开销
// var cost = function (money) {
// costMoney+=money;
// }
//
// cost(100);//第一天
// cost(200);//第二天
// cost(300);//第三天 ....
//改善之后 ,只关注 30号的账单 ,日常每天累计不关心
var costMoney = 0;
//函数自运行
var cost = (function(){
var arg = []; //用于记录每一天的花费
// console.log("这是一个cost返回的函数");
return function (x) {
if(x!=undefined){
arg.push(x);
}else{
console.log("开始计算结果:",arg);
arg.forEach(function(item){
// console.log(item);
costMoney+=item;
})
}
// console.log(arg);//此处可以使用到arg变量
};
})();
// console.log(cost);
cost(500); //第一天
cost(100);//第二天
cost(100);//第二天
cost(100);//第二天
cost(100);//第二天
// cost(); //最后结算